package com.xwdeshj.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xwdeshj.entity.XwSceincPlaytype;
import com.xwdeshj.entity.XwSceincThemetype;
import com.xwdeshj.repository.XwSceincThemetypeRepository;
import com.xwdeshj.service.XwSceincThemetypeService;
import com.xwdeshj.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by BKWJA on 2018/12/21.
 * 景区主题游类型实现类
 */
@Slf4j
@Service("xwSceincThemetypeService")
public class XwSceincThemetypeServiceImpl implements XwSceincThemetypeService {

    @Autowired
    private XwSceincThemetypeRepository xwSceincThemetypeRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private Sid sid;

    @Override
    public JSONObject getAllSceThemetype() {
        JSONObject result = new JSONObject();
        String sql = "select scenic_theme_Id,scenic_theme_encryptId,scenic_themee_desc from xw_sceinc_themetype order by scenic_theme_createtime ASC";
        try {
            SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
            JSONArray jsonArray = new JSONArray();
            while (rs.next()){
                JSONObject object = new JSONObject();
                object.put("themeId",rs.getString("scenic_theme_Id"));
                object.put("encryptId",rs.getString("scenic_theme_encryptId"));
                object.put("title",(rs.getString("scenic_themee_desc")==null?"":rs.getString("scenic_themee_desc")));
                jsonArray.add(object);
            }
            result.put("list",jsonArray);
            result.put("status",0);
            result.put("msg","ok");
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg",e);
        }
        System.out.println("getAllSceThemetype："+result.toString());
        return  result;
    }

    @Override
    public JSONObject getSceThemetypelist(Integer pageindex, Integer pagesize, String starttime,
                                          String endtime, String sortfild, String sortway) {
        JSONObject result = new JSONObject();
        String sql = "select scenic_theme_Id,scenic_theme_encryptId,scenic_themee_desc,scenic_theme_updatetime,scenic_theme_createtime from xw_sceinc_themetype ";
        StringBuffer sb = new StringBuffer();
        sb.append(" 1=1 ");
        if(!starttime.equals("")){
            starttime += " 00.00.01";
            endtime += " 23.59.59";
            sb.append(" and scenic_theme_createtime >= '"+starttime+"' and scenic_theme_createtime <= '"+ endtime + "' ");
        }
        //排序
        if (!sortfild.equals("")){
            sb.append(" order by "+sortfild+" " +sortway + " ");
        }
        try {
            sql = sql + " where " +sb.toString() + " limit "+ (pageindex-1)*pagesize + "," + pagesize;
            //分页SQL
            String sql2 = "select count(*) from xw_sceinc_themetype where " + sb.toString();
            SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
            SqlRowSet rs2 = jdbcTemplate.queryForRowSet(sql2);
            result.put("status",0);
            result.put("msg","ok");
            while (rs2.next()){
                Integer count = rs2.getInt(1);
                result.put("recordcount",count);
                if (count%pagesize == 0){
                    result.put("pagecount",count/pagesize);
                }else {
                    result.put("pagecount",count/pagesize + 1);
                }
            }
            JSONArray jsonArray = new JSONArray();
            while (rs.next()){
                JSONObject object = new JSONObject();
                object.put("scenic_theme_Id",rs.getString("scenic_theme_Id"));
                object.put("scenic_theme_encryptId",rs.getString("scenic_theme_encryptId"));
                object.put("scenic_themee_desc",(rs.getString("scenic_themee_desc")==null?"":rs.getString("scenic_themee_desc")));
                object.put("scenic_theme_updatetime", DateUtil.DateToString(rs.getDate("scenic_theme_updatetime")));
                object.put("scenic_theme_createtime",DateUtil.DateToString(rs.getDate("scenic_theme_createtime")));
                jsonArray.add(object);
            }
            result.put("list",jsonArray);
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg",e);
        }
        System.out.println("getSceThemetypelist:"+result.toString());
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject updateSceThemetype(String scenic_theme_encryptId, String scenic_themee_desc) {
        JSONObject result = new JSONObject();
        if(scenic_theme_encryptId.equals("")){
            XwSceincThemetype sceincThemetype = new XwSceincThemetype();
            sceincThemetype.setScenicThemeEncryptid(sid.nextShort());
            sceincThemetype.setScenicThemeeDesc(scenic_themee_desc);
            sceincThemetype.setScenicThemeCreatetime(new Date());
            sceincThemetype.setScenicThemeUpdatetime(new Date());

            try {
                xwSceincThemetypeRepository.save(sceincThemetype);
                result.put("status",0);
                result.put("msg","ok");
            }catch (Exception e){
                e.printStackTrace();
                result.put("status",500);
                result.put("msg",e);
            }
        }else {
            XwSceincThemetype xwSceincThemetype = xwSceincThemetypeRepository.findByScenicThemeEncryptid(scenic_theme_encryptId);

            if (xwSceincThemetype != null){
                xwSceincThemetype.setScenicThemeeDesc(scenic_themee_desc);
                xwSceincThemetype.setScenicThemeUpdatetime(new Date());
                try {
                    xwSceincThemetypeRepository.save(xwSceincThemetype);
                    result.put("status",0);
                    result.put("msg","ok");
                }catch (Exception e){
                    e.printStackTrace();
                    result.put("status",500);
                    result.put("msg",e);
                }
            }else {
                result.put("status",500);
                result.put("msg","该记录不存在！");
            }

        }
        System.out.println("updateSceThemetype:"+result.toString());
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject delSceThemetype(String scenic_theme_encryptId) {
        JSONObject result = new JSONObject();
        if(!"".equals(scenic_theme_encryptId) && scenic_theme_encryptId != null){
            try {
                if(scenic_theme_encryptId.indexOf(",") == -1){
                    //单个删除
                    int flag =  xwSceincThemetypeRepository.deleteByScenicThemeEncryptid(scenic_theme_encryptId);
                    if(flag == 1){
                        result.put("status",0);
                        result.put("msg","ok");
                    }else {
                        result.put("status",500);
                        result.put("msg","删除失败！");
                    }
                }else {
                    //批量删除
                    int flag = 0;
                    String[] tids = scenic_theme_encryptId.split(",");
                    for (int i = 0; i < tids.length; i++) {
                        flag = xwSceincThemetypeRepository.deleteByScenicThemeEncryptid(tids[i]);
                    }
                    if(flag == 1){
                        result.put("status",0);
                        result.put("msg","ok");
                    }else {
                        result.put("status",500);
                        result.put("msg","删除失败！");
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                result.put("status",500);
                result.put("msg",e);
            }

        }else {
            result.put("status",500);
            result.put("msg","scenic_theme_encryptId不能为空！");
        }
        System.out.println("delSceThemetype："+result);
        return result;
    }
}
